﻿using System.Net;
using System.Text;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Communication.Net.Udp;
using UtilZ.Dotnet.Ex.Log;

namespace WinFormApp
{
    public partial class FTestSNTChannel : Form
    {
        public FTestSNTChannel()
        {
            InitializeComponent();
        }


        private SNTChannel _scUdpClient = null;
        private IPEndPoint _dstEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3001);
        private void FTestSCUdpClient_Load(object sender, EventArgs e)
        {
            try
            {
                var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
                if (redirectAppenderToUI != null)
                {
                    redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
                }

                var initPara = new NetUdpClientInitPara(null, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000));
                this._scUdpClient = new SNTChannel(initPara, 100000, 30);
                this._scUdpClient.PackOutput += _scUdpClient_PackOutput;
                this._scUdpClient.RequestOutput += _scUdpClient_RequestOutput;
                Loger.Info("启动完成");
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }



        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            logControl1.AddLog(e.Item.Content, e.Item.Level);
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            logControl1.Clear();
        }



        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                this._scUdpClient.SendTo(Encoding.UTF8.GetBytes($"凤凰山自检运维及代理软件聂娜木及拉萨两点联调:{TimeEx.GetTimestamp()}"), this._dstEndPoint);
                Loger.Info("发送成功.");
            }
            catch (TimeoutException tex)
            {
                Loger.Warn(tex);
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private void _scUdpClient_PackOutput(object sender, UtilZ.Dotnet.Ex.Communication.Base.DataOutputArgs e)
        {
            Loger.Info($"收到数据: {Encoding.UTF8.GetString(e.Data.Bytes, e.Data.Offset, e.Data.Length)}");
        }

        private void _scUdpClient_RequestOutput(object sender, RequestDataOutputArgs e)
        {
            Loger.Info($"收到请求数据:{Encoding.UTF8.GetString(e.Data)}");
            e.Result.Update(Encoding.UTF8.GetBytes($"这是一条回复数据{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"));
        }

        private void btnRequest_Click(object sender, EventArgs e)
        {
            try
            {
                string result = this._scUdpClient.Request($"16G/万兆光口网卡/双千兆网口/研华原装整机，两年质保 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}", this._dstEndPoint);
                Loger.Info($"收到到回复数据:{result}");
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }
    }
}
